﻿// JScript 文件

Ext.onReady(function () {
    // 使用表单提示
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var sm = new Ext.grid.CheckboxSelectionModel();

    var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm, 
		{ header: '标题', width: 500, align: 'left', dataIndex: 'plaTitle' },
		{ header: '时间', width: 100, align: 'center', dataIndex: 'plaTime' },
        { header: '失效时间', width: 100, align: 'center', dataIndex: 'plaExpiryTime' },
        { header: '类型', width: 120, align: 'center', dataIndex: 'plaType',renderer:function(v){
                if(v=="0") 
                    return "来自编辑内容"; 
                else 
                    return "来自外部链接";
                } 
        }
        
        

	]);

    cm.defaultSortable = true;

    var ds = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: 'ashx/Placard_GetByPage.ashx'
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        },
		[{ name: 'id', mapping: 'plaID', type: 'string' }, 
		 { name: 'plaTitle', type: 'string' },
         { name: 'plaExpiryTime', type: 'string' },
		 { name: 'plaTime', type: 'string' },
		 { name: 'plaURL', type: 'string' },
         { name: 'plaUID', type: 'string' }, 
         { name: 'realName', type: 'string' },
         { name: 'plaType', type: 'string' }  
		])
    });

    var paging = new Ext.PagingToolbar({
        pageSize: 20,
        store: ds,
        displayInfo: true,
        displayMsg: '显示第{0}条到{1}条记录，一共{2}条',
        items:
		["-", " ",
		    { text: '添加',
		        tooltip: '添加一条公告',
		        iconCls: 'icon-add',
		        handler: function () {
		            window.location.href = "Placard_Add.aspx";
		        }
		    },
		"-", " ",
		    { text: '修改',
		        tooltip: '修改一条公告',
		        iconCls: 'icon-edit',
		        handler: function () {
		            var _record = grid.getSelectionModel().getSelected();
		            if (sm.getCount() == 1) {
		                window.location.href = "Placard_Add.aspx?pid=" + _record.data.id;
		            } else
		                Ext.Msg.alert('提示', '请选择一条记录！');

		        }
		    },
		"-", " ", { text: '删除',
		    tooltip: '删除选择的公告',
		    iconCls: 'icon-del',
		    handler: function () {

		        var _record = grid.getSelectionModel().getSelected();
		        if (sm.getCount() == 1) {
		            del_placard(_record);
		        } else
		            Ext.Msg.alert('提示', '请选择一条记录！');

		    }
		}
		],
        emptyMsg: "查询记录为空!"
    });

    var grid = new Ext.grid.EditorGridPanel({
        renderTo: 'grid-div',
        cm: cm,
        ds: ds,
        sm: sm, 
        bbar: paging,
        height: 500,
        border: true,
        bodyStyle: 'width:1020px',
        stripeRows: true,
        title: '公告管理'
    });

    ds.load({ params: { start: 0, limit: 20} });



    function del_placard(record) { 
        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        Ext.Ajax.request({
            url: 'ashx/Placard_Del.ashx',
            params:{
                 pid:record.data.id
            },
            success: function (response) {
                ds.load();
                myMask.hide();
            },
            failure: function () {

                Ext.Msg.alert('提示', '操作失败！');
                myMask.hide();
            }
        });
    }

 


});